
snippet "(\S+)\{" "" r
`!p snip.rv=match.group(1)`{$1}$0
endsnippet

snippet "(\s*)\{" "" r
`!p snip.rv=match.group(1)`{
`!p snip.rv=match.group(1)`	$1
`!p snip.rv=match.group(1)`}$0
endsnippet

snippet "(.*)\[" "" r
`!p snip.rv=match.group(1)`[$1]$0
endsnippet

snippet "(.*)\(" "" r
`!p snip.rv=match.group(1)`($1)$0
endsnippet

snippet "(.*)\"" "" r
`!p snip.rv=match.group(1)`"$1"$0
endsnippet

snippet "(.*)'" "" r
`!p snip.rv=match.group(1)`'$1'$0
endsnippet

snippet pk
printk(KERN_EMERG "${1}\n"${2});${0}
endsnippet

snippet module
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas");
static int __init ${1}_init(void);
static void __exit $1_exit(void);

$0

int __init $1_init(void)
{

	return 0;
}

void __exit $1_exit(void)
{
	printk(KERN_EMERG "module exit ok!\n");
}

module_init($1_init);
module_exit($1_exit);

endsnippet

snippet head
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <signal.h>
#include <sys/ioctl.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/time.h>
#include <pthread.h>
${0}
endsnippet

snippet atexit
if (atexit(${1:/*void *function*/}(${2:/*void*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- atexit failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet fopen
if ((${1:fp}=fopen(${2:/*const char *path*/}, ${3:/*const char *mode*/})) == NULL) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- open error: %s", __FILE__, __LINE__, __func__, $2, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet opendir
if ((${1:dir}=opendir(${2:/*const char *name*/})) == NULL) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- Opendir failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet open
if ((${1:fd}=open(${2:/*const char *pathname*/}, ${3:/*int flags*/}${4:, /*mode_t mode*/})) < 0){
	fprintf(stderr, "Chen: %s:%d %s?@0 -- open %s error: %s", __FILE__, __LINE__, __func__, $2, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet read
if ((${1:nResult}=read(${2:/*int fd*/}, ${3:/*void *buf*/}, ${4:/*size_t count*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- read error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet write
if ((${1:nResult}=write(${2:/*int fd*/}, ${3:/*void *buf*/}, ${4:/*size_t count*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- write error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet pread
if ((${1:nResult}=pread(${2:/*int fd*/}, ${3:/*void *buf*/}, ${4:/*size_t count*/}, ${5:/*off_t offset*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- pread error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet pwrite
if ((${1:nResult}=pwrite(${2:/*int fd*/}, ${3:/*const void *buf*/}, ${4:/*size_t count*/}, ${5:/*off_t offset*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- pwrite error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet fork
if ((${1:pid}=fork()) < 0)
{
	fprintf(stderr, "Chen: %s:%d %s?@0 -- fork error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}
if ($1 == 0) {
	${2}
} else {
	${3}
}${0}
endsnippet

snippet mkstemp
if ((${1:fd}=mkstemp(${2:/*char *template*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- mkstemp %s error: %s", __FILE__, __LINE__, __func__, $2, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet tmpfile
if ((${1:fp}=tmpfile()) == NULL) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- tmpfile error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet stat
if (stat(${1:/*const char *path*/}, ${2:/*struct stat *buf*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- stat %s error: %s\n", __FILE__, __LINE__, __func__, $1, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet fstat
if (fstat(${1:/*int fd*/}, ${2:/*struct stat *buf*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- fstat error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet lstat
if (lstat(${1:/*const char *path*/}, ${2:/*struct stat *buf*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- lstat %s error: %s\n", __FILE__, __LINE__, __func__, $1, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet rename
if (rename(${1:/*const char *oldpath*/}, ${2:/*const char *newpath*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- rename %s to %s failed: %s\n", __FILE__, __LINE__, __func__, $1, $2, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet lseek
if (lseek(${1:/*int fd*/}, ${2:/*off_t offset*/}, ${3:/*int whence*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- lseek error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet fseek
if (fseek(${1:fp}, 0L, SEEK_SET) < 0)
{
	fprintf(stderr, "Chen: %s:%d %s?@0 -- fseek error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet pthread_create
if (${1:/*ret=*/}pthread_create(${2:/*pthread_t *thread*/}, ${3:/*const pthread_attr_t *attr*/}, ${4:/*void *(*start_routine) (void *)*/}, ${5:/*void *arg*/}) < 0){
	fprintf(stderr, "Chen: %s:%d %s?@0 -- pthread_create failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet semget
if ((semid=semget(${1:/*key_t key*/}, ${2:/*int nsems*/}, ${3:/*int semflg*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- semget failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet semop
if (semop(${1:/*int semid*/}, ${2:/*struct sembuf *sops*/}, ${3:/*unsigned nsops*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- semop failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet semctl
if (semctl(${1:/*int semid*/}, ${2:/*int semnum*/}, ${3:/*int cmd*/}, ${4:/*...*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- semctl failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet shmget
if ((${1:shmid} = shmget(${1:/*key_t key*/}, ${2:/*size_t size*/}, ${3:/*int shmflg*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- shmget failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet shmat "attatch"
if ((${1:shmptr} = shmat(${2:/*int shmid*/}, ${3:/*const void *shmaddr*/}, ${4:/*int shmflg*/})) == (void *)-1) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- shmat failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet shmdt "Detach"
if (shmdt(${1:/*const void *shmaddr*/}) == -1) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- shmdt failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet pipe
if (pipe(${1:fd}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- pipe failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet nResult
if (nResult < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- ${1:read} file error: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet msgget
if ((${1:msqid}=msgget(${2:/*key_t key*/}, ${3:IPC_CREAT|0664})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- msgget failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet msgbuf
struct msgbuf {
	long mtype;
	char mtext[${1:256}];
};${0}
endsnippet

snippet msgsnd
if (msgsnd(${1:/*int msqid*/}, ${2:/*const void *msgp*/}, ${3:/*size_t msgsz*/}, ${4:/*int msgflg*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- msgsnd failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet msgrcv
if ((${1:nResult}=msgrcv(${2:/*int msqid*/}, ${3:/*void *msgp*/}, ${4:/*size_t msgsz*/}, ${5:/*long msgtyp*/}, ${6:/*int msgflg*/})) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- msgrcv failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet msgctl
if (msgctl(${1:/*int msqid*/}, ${2:/*int cmd*/}, ${3:/*struct msqid_ds *buf*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- msgctl failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet mkfifo
if (mkfifo(${1:/*const char *pathname*/}, ${2:/*mode_t mode*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- mkfifo failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet gettimeofday "microsecond"
if (gettimeofday(${1:/*struct timeval *tv*/}, ${2:/*struct timezone *tz*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- gettimeofday failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet atoi
if (atoi(${1:/*const char *nptr*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- atoi failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet atol
if (atol(${1:/*const char *nptr*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- atol failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet access "F_OK/W_OK/R_OK/X_OK -- check"
if (access(${1:/*const char *pathname*/}, ${2:/*int mode*/}) < 0) {
	fprintf(stderr, "Chen: %s:%d %s?@0 -- access failed: %s", __FILE__, __LINE__, __func__, strerror(errno));
	exit(EXIT_FAILURE);
}${0}
endsnippet

snippet ftok
key_t ${1:key} = ftok(${2:/*const char *pathname*/}, ${3:/*int proj_id*/});${0}
endsnippet

snippet	close
close(${1:fd});${0}
endsnippet

snippet	fclose
fclose(${1:fp});${0}
endsnippet

snippet memset
memset(${1:/*void *s*/}, ${2:/*int c*/}, ${3:/*size_t n*/});${4}
endsnippet

snippet fgets
fgets(${1:/*char *s*/}, ${2:/*int size*/}, ${3:/*FILE *stream*/});${4}
endsnippet

snippet unlink
unlink(${1:/*const char *pathname*/});${2}
endsnippet

snippet sprintf
sprintf(${1:/*char *str*/}, ${2:/*const char *format*/});${0}
endsnippet

snippet snprintf
snprintf(${1:/*char *str*/}, ${2:/*size_t size*/}, ${3:/*const char *format*/});${0}
endsnippet

snippet strcpy
strcpy(${1:/*char *dest*/}, ${2:/*const char *src*/})${0}
endsnippet

snippet strcat
strcat(${1:/*char *dest*/}, ${2:/*const char *src*/})${0}
endsnippet

snippet strncpy
strncpy(${1:/*char *dest*/}, ${2:/*const char *src*/}, ${3:/*size_t n*/})${0}
endsnippet

snippet strncat
strncat(${1:/*char *dest*/}, ${2:/*const char *src*/}, ${3:/*size_t n*/})${0}
endsnippet

snippet strncpy
strncpy(${1:/*char *dest*/}, ${2:/*const char *src*/}, ${3:/*size_t n*/})${0}
endsnippet

#(${1:/*void **/})malloc(sizeof(${1/.*\([0-9a-zA-Z]* \*\).*/\1/}));${0}
# (${1:/*void*/} *)malloc(sizeof(${1/(^\/\*|\*\/$)//g}));${0}
snippet malloc
$1 = (${2:/*void*/} *)malloc(sizeof(${2/(^\/\*|\*\/$)//g}));
if ($1 == NULL)
{
	// ENOMEM 12 Cannot allocate memory
}
${0}
endsnippet

snippet memcpy
memcpy(${1:/*void *dest*/}, ${2:/*const void *src*/}, ${3:/*size_t n*/});${0}
endsnippet

